package org.nonsense;
import javax.swing.JOptionPane;

class PlaybackThread extends Thread {
	byte tempBuffer[] = new byte[10000];

	private RecorderModel recorderModel;

	public PlaybackThread(RecorderModel rm)	{
		recorderModel = rm;
	}
	
	public void run() {
		try {
			int cnt;
			// Keep looping until the input read method
			// returns -1 for empty stream.
			while ((cnt = recorderModel.getAudioInputStream().read(tempBuffer, 0,
					tempBuffer.length)) != -1) {
				if (cnt > 0) {
					// Write data to the internal buffer of
					// the data line where it will be
					// delivered to the speaker.
					recorderModel.getSourceDataLine().write(tempBuffer, 0, cnt);
				}
			}

			System.out.println("Playback complete.");
			
			// Block and wait for internal buffer of the
			// data line to empty.
			recorderModel.getSourceDataLine().drain();
			recorderModel.getSourceDataLine().close();
		} catch (Exception e) {
			System.out.println(e);
			System.exit(0);
		}
	}
}


